home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / util / misc / Fudgit233.lha / Source / src / readline / history.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-14  |  36.8 KB  |  1,540 lines

  1. /* History.c -- standalone history library */
  2.  
  3. /* Copyright (C) 1989 Free Software Foundation, Inc.
  4.  
  5.    This file contains the GNU History Library (the Library), a set of
  6.    routines for managing the text of previously typed lines.
  7.  
  8.    The Library is free software; you can redistribute it and/or modify
  9.    it under the terms of the GNU General Public License as published by
  10.    the Free Software Foundation; either version 1, or (at your option)
  11.    any later version.
  12.  
  13.    The Library is distributed in the hope that it will be useful, but
  14.    WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.    General Public License for more details.
  17.  
  18.    The GNU General Public License is often shipped with GNU software, and
  19.    is generally kept in a file called COPYING or LICENSE.  If you do not
  20.    have a copy of the license, write to the Free Software Foundation,
  21.    675 Mass Ave, Cambridge, MA 02139, USA. */
  22.  
  23. /* The goal is to make the implementation transparent, so that you
  24.    don't have to know what data types are used, just what functions
  25.    you can call.  I think I have done that. */
  26.  
  27. /* Remove these declarations when we have a complete libgnu.a. */
  28. #define STATIC_MALLOC
  29. #ifndef STATIC_MALLOC
  30. extern char *xmalloc (void *), *xrealloc (void *, int);
  31. #else
  32. static char *xmalloc (int bytes), *xrealloc (void *pointer, int bytes);
  33. #endif
  34.  
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <sys/types.h>
  38. #include <sys/file.h>
  39. #include <sys/stat.h>
  40. #include <fcntl.h>
  41.  
  42. #ifdef __GNUC__
  43. #define alloca __builtin_alloca
  44. #else
  45. #if defined (sparc) || defined (sgi)
  46. #include <alloca.h>
  47. #else
  48. extern char *alloca ();
  49. #endif
  50. #endif
  51.  
  52. #include "history.h"
  53.  
  54. #ifndef savestring
  55. #define savestring(x) (char *)strcpy (xmalloc (1 + strlen (x)), (x))
  56. #endif
  57.  
  58. #ifndef whitespace
  59. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  60. #endif
  61.  
  62. #ifndef digit
  63. #define digit(c)  ((c) >= '0' && (c) <= '9')
  64. #endif
  65.  
  66. #ifndef member
  67. #define member(c, s) ((c) ? strchr ((s), (c)) : 0)
  68. #endif
  69.  
  70. /* **************************************************************** */
  71. /*                                    */
  72. /*            History Functions                */
  73. /*                                    */
  74. /* **************************************************************** */
  75.  
  76. /* An array of HIST_ENTRY.  This is where we store the history. */
  77. static HIST_ENTRY **the_history = (HIST_ENTRY **)NULL;
  78.  
  79. /* Non-zero means that we have enforced a limit on the amount of
  80.    history that we save. */
  81. int hl_history_stifled = 0;
  82.  
  83. /* If HISTORY_STIFLED is non-zero, then this is the maximum number of
  84.    entries to remember. */
  85. int hl_max_input_history;
  86.  
  87. /* The current location of the interactive history pointer.  Just makes
  88.    life easier for outside callers. */
  89. static int history_offset = 0;
  90.  
  91. /* The number of strings currently stored in the input_history list. */
  92. int hl_history_length = 0;
  93.  
  94. /* The current number of slots allocated to the input_history. */
  95. static int history_size = 0;
  96.  
  97. /* The number of slots to increase the_history by. */
  98. #define DEFAULT_HISTORY_GROW_SIZE 50
  99.  
  100. /* The character that represents the start of a history expansion
  101.    request.  This is usually `!'. */
  102. char hl_history_expansion_char = '!';
  103.  
  104. /* The character that invokes word substitution if found at the start of
  105.    a line.  This is usually `^'. */
  106. char hl_history_subst_char = '^';
  107.  
  108. /* During tokenization, if this character is seen as the first character
  109.    of a word, then it, and all subsequent characters upto a newline are
  110.    ignored.  For a Bourne shell, this should be '#'.  Bash special cases
  111.    the interactive comment character to not be a comment delimiter. */
  112. char hl_history_comment_char = '\0';
  113.  
  114. /* The list of characters which inhibit the expansion of text if found
  115.    immediately following hl_history_expansion_char. */
  116. char *hl_history_no_expand_chars = " \t\n\r=";
  117.  
  118. /* The logical `base' of the history array.  It defaults to 1. */
  119. int hl_history_base = 1;
  120.  
  121. /* Begin a session in which the history functions might be used.  This
  122.    initializes interactive variables. */
  123.  
  124. extern void free (void *);
  125. extern void abort (void);
  126. extern char *getenv (const char *);
  127. extern int read (int, void *, unsigned int);
  128. extern int close (int);
  129. extern int write (int, const char *, unsigned int);
  130. extern void *malloc (size_t);
  131. extern void *realloc (void *, size_t);
  132. static char **history_tokenize (char *string);
  133. static char *get_history_word_specifier (char *spec, char *from, int *caller_index);
  134.  
  135. void
  136. hl_using_history (void)
  137. {
  138.   history_offset = hl_history_length;
  139. }
  140.  
  141. /* Return the number of bytes that the primary history entries are using.
  142.    This just adds up the lengths of the_history->lines. */
  143. int
  144. hl_history_total_bytes (void)
  145. {
  146.   register int i, result;
  147.  
  148.   for (i = 0; the_history && the_history[i]; i++)
  149.     result += strlen (the_history[i]->line);
  150.  
  151.   return (result);
  152. }
  153.  
  154. /* Place STRING at the end of the history list.  The data field
  155.    is  set to NULL. */
  156. void
  157. hl_add_history (char *string)
  158. {
  159.   HIST_ENTRY *temp;
  160.  
  161.   if (hl_history_stifled && (hl_history_length == hl_max_input_history))
  162.     {
  163.       register int i;
  164.  
  165.       /* If the history is stifled, and hl_history_length is zero,
  166.      and it equals hl_max_input_history, we don't save items. */
  167.       if (!hl_history_length)
  168.     return;
  169.  
  170.       /* If there is something in the slot, then remove it. */
  171.       if (the_history[0])
  172.     {
  173.       free (the_history[0]->line);
  174.       free (the_history[0]);
  175.     }
  176.  
  177.       for (i = 0; i < hl_history_length; i++)
  178.     the_history[i] = the_history[i + 1];
  179.  
  180.       hl_history_base++;
  181.  
  182.     }
  183.   else
  184.     {
  185.       if (!history_size)
  186.     {
  187.       the_history = (HIST_ENTRY **)
  188.         xmalloc ((history_size = DEFAULT_HISTORY_GROW_SIZE)
  189.              * sizeof (HIST_ENTRY *));
  190.       hl_history_length = 1;
  191.  
  192.     }
  193.       else
  194.     {
  195.       if (hl_history_length == (history_size - 1))
  196.         {
  197.           the_history = (HIST_ENTRY **)
  198.         xrealloc (the_history,
  199.               ((history_size += DEFAULT_HISTORY_GROW_SIZE)
  200.                * sizeof (HIST_ENTRY *)));
  201.       }
  202.       hl_history_length++;
  203.     }
  204.     }
  205.  
  206.   temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  207.   temp->line = savestring (string);
  208.   temp->data = (char *)NULL;
  209.  
  210.   the_history[hl_history_length] = (HIST_ENTRY *)NULL;
  211.   the_history[hl_history_length - 1] = temp;
  212. }
  213.  
  214. /* Make the history entry at WHICH have LINE and DATA.  This returns
  215.    the old entry so you can dispose of the data.  In the case of an
  216.    invalid WHICH, a NULL pointer is returned. */
  217. HIST_ENTRY *
  218. hl_replace_history_entry (int which, char *line, char *data)
  219. {
  220.   HIST_ENTRY *temp = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  221.   HIST_ENTRY *old_value;
  222.  
  223.   if (which >= hl_history_length)
  224.     return ((HIST_ENTRY *)NULL);
  225.  
  226.   old_value = the_history[which];
  227.  
  228.   temp->line = savestring (line);
  229.   temp->data = data;
  230.   the_history[which] = temp;
  231.  
  232.   return (old_value);
  233. }
  234.  
  235. /* Returns the magic number which says what history element we are
  236.    looking at now.  In this implementation, it returns history_offset. */
  237. int
  238. hl_where_history (void)
  239. {
  240.   return (history_offset);
  241. }
  242.  
  243. /* Search the history for STRING, starting at history_offset.
  244.    If DIRECTION < 0, then the search is through previous entries,
  245.    else through subsequent.  If the string is found, then
  246.    current_history () is the history entry, and the value of this function
  247.    is the offset in the line of that history entry that the string was
  248.    found in.  Otherwise, nothing is changed, and a -1 is returned. */
  249. int
  250. hl_history_search (char *string, int direction)
  251. {
  252.   register int i = history_offset;
  253.   register int reverse = (direction < 0);
  254.   register char *line;
  255.   register int hindex;
  256.   int string_len = strlen (string);
  257.  
  258.   /* Take care of trivial cases first. */
  259.  
  260.   if (!hl_history_length || ((i == hl_history_length) && !reverse))
  261.     return (-1);
  262.  
  263.   if (reverse && (i == hl_history_length))
  264.     i--;
  265.  
  266.   while (1)
  267.     {
  268.       /* Search each line in the history list for STRING. */
  269.  
  270.       /* At limit for direction? */
  271.       if ((reverse && i < 0) ||
  272.       (!reverse && i == hl_history_length))
  273.     return (-1);
  274.  
  275.       line = the_history[i]->line;
  276.       hindex = strlen (line);
  277.  
  278.       /* If STRING is longer than line, no match. */
  279.       if (string_len > hindex)
  280.     goto next_line;
  281.  
  282.       /* Do the actual search. */
  283.       if (reverse)
  284.     {
  285.       hindex -= string_len;
  286.  
  287.       while (hindex >= 0)
  288.         {
  289.           if (strncmp (string, line + hindex, string_len) == 0)
  290.         {
  291.           history_offset = i;
  292.           return (hindex);
  293.         }
  294.           hindex--;
  295.         }
  296.     }
  297.       else
  298.     {
  299.       register int limit = (string_len - hindex) + 1;
  300.       hindex = 0;
  301.  
  302.       while (hindex < limit)
  303.         {
  304.           if (strncmp (string, line + hindex, string_len) == 0)
  305.         {
  306.           history_offset = i;
  307.           return (hindex);
  308.         }
  309.           hindex++;
  310.         }
  311.     }
  312.     next_line:
  313.       if (reverse)
  314.     i--;
  315.       else
  316.     i++;
  317.     }
  318. }
  319.  
  320. /* Remove history element WHICH from the history.  The removed
  321.    element is returned to you so you can free the line, data,
  322.    and containing structure. */
  323. HIST_ENTRY *
  324. hl_remove_history (int which)
  325. {
  326.   HIST_ENTRY *return_value;
  327.  
  328.   if (which >= hl_history_length || !hl_history_length)
  329.     return_value = (HIST_ENTRY *)NULL;
  330.   else
  331.     {
  332.       register int i;
  333.       return_value = the_history[which];
  334.  
  335.       for (i = which; i < hl_history_length; i++)
  336.     the_history[i] = the_history[i + 1];
  337.  
  338.       hl_history_length--;
  339.     }
  340.  
  341.   return (return_value);
  342. }
  343.  
  344. /* Stifle the history list, remembering only MAX number of lines. */
  345. void
  346. hl_stifle_history (int max)
  347. {
  348.   if (hl_history_length > max)
  349.     {
  350.       register int i, j;
  351.  
  352.       /* This loses because we cannot free the data. */
  353.       for (i = 0; i < (hl_history_length - max); i++)
  354.     {
  355.       free (the_history[i]->line);
  356.       free (the_history[i]);
  357.     }
  358.       hl_history_base = i;
  359.       for (j = 0, i = hl_history_length - max; j < max; i++, j++)
  360.     the_history[j] = the_history[i];
  361.       the_history[j] = (HIST_ENTRY *)NULL;
  362.       hl_history_length = j;
  363.     }
  364.   hl_history_stifled = 1;
  365.   hl_max_input_history = max;
  366. }
  367.  
  368. /* Stop stifling the history.  This returns the previous amount the history
  369.  was stifled by.  The value is positive if the history was stifled, negative
  370.  if it wasn't. */
  371. int
  372. hl_unstifle_history (void)
  373. {
  374.   int result = hl_max_input_history;
  375.   if (hl_history_stifled)
  376.     {
  377.       result = - result;
  378.       hl_history_stifled = 0;
  379.     }
  380.   return (result);
  381. }
  382.  
  383. /* Return the string that should be used in the place of this
  384.    filename.  This only matters when you don't specify the
  385.    filename to read_history (), or write_history (). */
  386. static char *
  387. history_filename (char *filename)
  388. {
  389.   char *return_val = filename ? savestring (filename) : (char *)NULL;
  390.  
  391.   if (!return_val)
  392.     {
  393.       char *home = (char *)getenv ("HOME");
  394.       if (!home) home = ".";
  395.       return_val = (char *)xmalloc (2 + strlen (home) + strlen (".history"));
  396.       sprintf (return_val, "%s/.history", home);
  397.     }
  398.   return (return_val);
  399. }
  400.  
  401. /* Add the contents of FILENAME to the history list, a line at a time.
  402.    If FILENAME is NULL, then read from ~/.history.  Returns 0 if
  403.    successful, or errno if not. */
  404. int
  405. hl_read_history (char *filename)
  406. {
  407.   return (hl_read_history_range (filename, 0, -1));
  408. }
  409.  
  410. /* Read a range of lines from FILENAME, adding them to the history list.
  411.    Start reading at the FROM'th line and end at the TO'th.  If FROM
  412.    is zero, start at the beginning.  If TO is less than FROM, read
  413.    until the end of the file.  If FILENAME is NULL, then read from
  414.    ~/.history.  Returns 0 if successful, or errno if not. */
  415. int
  416. hl_read_history_range (char *filename, int from, int to)
  417. {
  418.   register int line_start, line_end;
  419.   char *input, *buffer = (char *)NULL;
  420.   int file, current_line;
  421.   struct stat finfo;
  422.   extern int errno;
  423.  
  424.   input = history_filename (filename);
  425.   file = open (input, O_RDONLY, 0666);
  426.  
  427.   if ((file < 0) ||
  428.       (stat (input, &finfo) == -1))
  429.     goto error_and_exit;
  430.  
  431.   buffer = (char *)xmalloc (finfo.st_size + 1);
  432.  
  433.   if (read (file, buffer, finfo.st_size) != finfo.st_size)
  434.   error_and_exit:
  435.     {
  436.       if (file >= 0)
  437.     close (file);
  438.  
  439.       if (buffer)
  440.     free (buffer);
  441.  
  442.       return (errno);
  443.     }
  444.  
  445.   close (file);
  446.  
  447.   /* Set TO to larger than end of file if negative. */
  448.   if (to < 0)
  449.     to = finfo.st_size;
  450.  
  451.   /* Start at beginning of file, work to end. */
  452.   line_start = line_end = current_line = 0;
  453.  
  454.   /* Skip lines until we are at FROM. */
  455.   while (line_start < finfo.st_size && current_line < from)
  456.     {
  457.       for (line_end = line_start; line_end < finfo.st_size; line_end++)
  458.     if (buffer[line_end] == '\n')
  459.       {
  460.         current_line++;
  461.         line_start = line_end + 1;
  462.         if (current_line == from)
  463.           break;
  464.       }
  465.     }
  466.  
  467.   /* If there are lines left to gobble, then gobble them now. */
  468.   for (line_end = line_start; line_end < finfo.st_size; line_end++)
  469.     if (buffer[line_end] == '\n')
  470.       {
  471.     buffer[line_end] = '\0';
  472.  
  473.     if (buffer[line_start])
  474.       hl_add_history (buffer + line_start);
  475.  
  476.     current_line++;
  477.  
  478.     if (current_line >= to)
  479.       break;
  480.  
  481.     line_start = line_end + 1;
  482.       }
  483.   return (0);
  484. }
  485.  
  486. /* Truncate the history file FNAME, leaving only LINES trailing lines.
  487.    If FNAME is NULL, then use ~/.history. */
  488. void
  489. hl_history_truncate_file (char *fname, register int lines)
  490. {
  491.   register int i;
  492.   int file;
  493.   char *buffer = (char *)NULL, *filename;
  494.   struct stat finfo;
  495.  
  496.   filename = history_filename (fname);
  497.   if (stat (filename, &finfo) == -1)
  498.     goto truncate_exit;
  499.  
  500.   file = open (filename, O_RDONLY, 066);
  501.  
  502.   if (file == -1)
  503.     goto truncate_exit;
  504.  
  505.   buffer = (char *)xmalloc (finfo.st_size + 1);
  506.   read (file, buffer, finfo.st_size);
  507.   close (file);
  508.  
  509.   /* Count backwards from the end of buffer until we have passed
  510.      LINES lines. */
  511.   for (i = finfo.st_size; lines && i; i--)
  512.     {
  513.       if (buffer[i] == '\n')
  514.     lines--;
  515.     }
  516.  
  517.   /* If there are fewer lines in the file than we want to truncate to,
  518.      then we are all done. */
  519.   if (!i)
  520.     goto truncate_exit;
  521.  
  522.   /* Otherwise, write from the start of this line until the end of the
  523.      buffer. */
  524.   for (--i; i; i--)
  525.     if (buffer[i] == '\n')
  526.       {
  527.     i++;
  528.     break;
  529.       }
  530.  
  531.   file = open (filename, O_WRONLY | O_TRUNC | O_CREAT, 0666);
  532.   if (file == -1)
  533.     goto truncate_exit;
  534.  
  535.   write (file, buffer + i, (unsigned) finfo.st_size - i);
  536.   close (file);
  537.  
  538.  truncate_exit:
  539.   if (buffer)
  540.     free (buffer);
  541.  
  542.   free (filename);
  543. }
  544.  
  545. #define HISTORY_APPEND 0
  546. #define HISTORY_OVERWRITE 1
  547.  
  548. /* Workhorse function for writing history.  Writes NELEMENT entries
  549.    from the history list to FILENAME.  OVERWRITE is non-zero if you
  550.    wish to replace FILENAME with the entries. */
  551. static int
  552. history_do_write (char *filename, int nelements, int overwrite)
  553. {
  554.   extern int errno;
  555.   register int i;
  556.   char *output = history_filename (filename);
  557.   int file, mode;
  558.   char cr = '\n';
  559.  
  560.   if (overwrite)
  561.     mode = O_WRONLY | O_CREAT | O_TRUNC;
  562.   else
  563.     mode = O_WRONLY | O_APPEND;
  564.  
  565.   if ((file = open (output, mode, 0666)) == -1)
  566.     return (errno);
  567.  
  568.   if (nelements > hl_history_length)
  569.     nelements = hl_history_length;
  570.  
  571.   for (i = hl_history_length - nelements; i < hl_history_length; i++)
  572.     {
  573.       if (write (file, the_history[i]->line,
  574.       (unsigned)strlen (the_history[i]->line)) < 0)
  575.     break;
  576.       if (write (file, &cr, (unsigned)1) < 0)
  577.     break;
  578.     }
  579.  
  580.   close (file);
  581.   return (0);
  582. }
  583.   
  584. /* Append NELEMENT entries to FILENAME.  The entries appended are from
  585.    the end of the list minus NELEMENTs up to the end of the list. */
  586. int
  587. hl_append_history (int nelements, char *filename)
  588. {
  589.   return (history_do_write (filename, nelements, HISTORY_APPEND));
  590. }
  591.  
  592. /* Overwrite FILENAME with the current history.  If FILENAME is NULL,
  593.    then write the history list to ~/.history.  Values returned
  594.    are as in read_history ().*/
  595. int
  596. hl_write_history (char *filename)
  597. {
  598.   return (history_do_write (filename, hl_history_length, HISTORY_OVERWRITE));
  599. }
  600.  
  601. /* Return the history entry at the current position, as determined by
  602.    history_offset.  If there is no entry there, return a NULL pointer. */
  603. HIST_ENTRY *
  604. hl_current_history (void)
  605. {
  606.   if ((history_offset == hl_history_length) || !the_history)
  607.     return ((HIST_ENTRY *)NULL);
  608.   else
  609.     return (the_history[history_offset]);
  610. }
  611.  
  612. /* Back up history_offset to the previous history entry, and return
  613.    a pointer to that entry.  If there is no previous entry then return
  614.    a NULL pointer. */
  615. HIST_ENTRY *
  616. hl_previous_history (void)
  617. {
  618.   if (!history_offset)
  619.     return ((HIST_ENTRY *)NULL);
  620.   else
  621.     return (the_history[--history_offset]);
  622. }
  623.  
  624. /* Move history_offset forward to the next history entry, and return
  625.    a pointer to that entry.  If there is no next entry then return a
  626.    NULL pointer. */
  627. HIST_ENTRY *
  628. hl_next_history (void)
  629. {
  630.   if (history_offset == hl_history_length)
  631.     return ((HIST_ENTRY *)NULL);
  632.   else
  633.     return (the_history[++history_offset]);
  634. }
  635.  
  636. /* Return the current history array.  The caller has to be carefull, since this
  637.    is the actual array of data, and could be bashed or made corrupt easily.
  638.    The array is terminated with a NULL pointer. */
  639. HIST_ENTRY **
  640. hl_history_list (void)
  641. {
  642.   return (the_history);
  643. }
  644.  
  645. /* Return the history entry which is logically at OFFSET in the history array.
  646.    OFFSET is relative to hl_history_base. */
  647. HIST_ENTRY *
  648. hl_history_get (int offset)
  649. {
  650.   int hindex = offset - hl_history_base;
  651.  
  652.   if (hindex >= hl_history_length ||
  653.       hindex < 0 ||
  654.       !the_history)
  655.     return ((HIST_ENTRY *)NULL);
  656.   return (the_history[hindex]);
  657. }
  658.  
  659. /* Search for STRING in the history list.  DIR is < 0 for searching
  660.    backwards.  POS is an absolute index into the history list at
  661.    which point to begin searching. */
  662. int
  663. hl_history_search_pos (char *string, int dir, int pos)
  664. {
  665.   int ret, old = hl_where_history ();
  666.   hl_history_set_pos (pos);
  667.   if (hl_history_search (string, dir) == -1)
  668.     {
  669.       hl_history_set_pos (old);
  670.       return (-1);
  671.     }
  672.   ret = hl_where_history ();
  673.   hl_history_set_pos (old);
  674.   return ret;
  675. }
  676.  
  677. /* Make the current history item be the one at POS, an absolute index.
  678.    Returns zero if POS is out of range, else non-zero. */
  679. int
  680. hl_history_set_pos (int pos)
  681. {
  682.   if (pos > hl_history_length || pos < 0 || !the_history)
  683.     return (0);
  684.   history_offset = pos;
  685.   return (1);
  686. }
  687.  
  688.  
  689. /* **************************************************************** */
  690. /*                                    */
  691. /*            History Expansion                */
  692. /*                                    */
  693. /* **************************************************************** */
  694.  
  695. /* Hairy history expansion on text, not tokens.  This is of general
  696.    use, and thus belongs in this library. */
  697.  
  698. /* The last string searched for in a !?string? search. */
  699. static char *search_string = (char *)NULL;
  700.  
  701. /* Return the event specified at TEXT + OFFSET modifying OFFSET to
  702.    point to after the event specifier.  Just a pointer to the history
  703.    line is returned; NULL is returned in the event of a bad specifier.
  704.    You pass STRING with *INDEX equal to the hl_history_expansion_char that
  705.    begins this specification.
  706.    DELIMITING_QUOTE is a character that is allowed to end the string
  707.    specification for what to search for in addition to the normal
  708.    characters `:', ` ', `\t', `\n', and sometimes `?'.
  709.    So you might call this function like:
  710.    line = get_history_event ("!echo:p", &index, 0);  */
  711. char *
  712. hl_get_history_event (char *string, int *caller_index, int delimiting_quote)
  713. {
  714.   register int i = *caller_index;
  715.   int which, sign = 1;
  716.   HIST_ENTRY *entry;
  717.  
  718.   /* The event can be specified in a number of ways.
  719.  
  720.      !!   the previous command
  721.      !n   command line N
  722.      !-n  current command-line minus N
  723.      !str the most recent command starting with STR
  724.      !?str[?]
  725.       the most recent command containing STR
  726.  
  727.      All values N are determined via HISTORY_BASE. */
  728.  
  729.   if (string[i] != hl_history_expansion_char)
  730.     return ((char *)NULL);
  731.  
  732.   /* Move on to the specification. */
  733.   i++;
  734.  
  735.   /* Handle !! case. */
  736.   if (string[i] == hl_history_expansion_char)
  737.     {
  738.       i++;
  739.       which = hl_history_base + (hl_history_length - 1);
  740.       *caller_index = i;
  741.       goto get_which;
  742.     }
  743.  
  744.   /* Hack case of numeric line specification. */
  745.   if (string[i] == '-')
  746.     {
  747.       sign = -1;
  748.       i++;
  749.     }
  750.  
  751.   if (digit (string[i]))
  752.     {
  753.       int start = i;
  754.  
  755.       /* Get the extent of the digits. */
  756.       for (; digit (string[i]); i++);
  757.  
  758.       /* Get the digit value. */
  759.       sscanf (string + start, "%d", &which);
  760.  
  761.       *caller_index = i;
  762.  
  763.       if (sign < 0)
  764.     which = (hl_history_length + hl_history_base) - which;
  765.  
  766.     get_which:
  767.       if (entry = hl_history_get (which))
  768.     return (entry->line);
  769.  
  770.       return ((char *)NULL);
  771.     }
  772.  
  773.   /* This must be something to search for.  If the spec begins with
  774.      a '?', then the string may be anywhere on the line.  Otherwise,
  775.      the string must be found at the start of a line. */
  776.   {
  777.     int hindex;
  778.     char *temp;
  779.     int substring_okay = 0;
  780.  
  781.     if (string[i] == '?')
  782.       {
  783.     substring_okay++;
  784.     i++;
  785.       }
  786.  
  787.     for (hindex = i; string[i]; i++)
  788.       if (whitespace (string[i]) ||
  789.       string[i] == '\n' ||
  790.       string[i] == ':' ||
  791.       (substring_okay && string[i] == '?') ||
  792.       string[i] == delimiting_quote)
  793.     break;
  794.  
  795.     temp = (char *)alloca (1 + (i - hindex));
  796.     strncpy (temp, &string[hindex], (i - hindex));
  797.     temp[i - hindex] = '\0';
  798.  
  799.     if (string[i] == '?')
  800.       i++;
  801.  
  802.     *caller_index = i;
  803.  
  804.   search_again:
  805.  
  806.     hindex = hl_history_search (temp, -1);
  807.  
  808.     if (hindex < 0)
  809.     search_lost:
  810.       {
  811.     history_offset = hl_history_length;
  812.     return ((char *)NULL);
  813.       }
  814.  
  815.     if (hindex == 0 || substring_okay || 
  816.     (strncmp (temp, the_history[history_offset]->line,
  817.           strlen (temp)) == 0))
  818.       {
  819.     entry = hl_current_history ();
  820.     history_offset = hl_history_length;
  821.     
  822.     /* If this was a substring search, then remember the string that
  823.        we matched for word substitution. */
  824.     if (substring_okay)
  825.       {
  826.         if (search_string)
  827.           free (search_string);
  828.         search_string = savestring (temp);
  829.       }
  830.  
  831.     return (entry->line);
  832.       }
  833.  
  834.     if (history_offset)
  835.       history_offset--;
  836.     else
  837.       goto search_lost;
  838.     
  839.     goto search_again;
  840.   }
  841. }
  842.  
  843. /* Expand the string STRING, placing the result into OUTPUT, a pointer
  844.    to a string.  Returns:
  845.  
  846.    0) If no expansions took place (or, if the only change in
  847.       the text was the de-slashifying of the history expansion
  848.       character)
  849.    1) If expansions did take place
  850.   -1) If there was an error in expansion.
  851.  
  852.   If an error ocurred in expansion, then OUTPUT contains a descriptive
  853.   error message. */
  854. int
  855. hl_history_expand (char *string, char **output)
  856. {
  857.   register int j, l = strlen (string);
  858.   int i, word_spec_error = 0;
  859.   int cc, modified = 0;
  860.   char *word_spec, *event;
  861.   int starting_index, only_printing = 0, substitute_globally = 0;
  862.  
  863.   char *strrchr (const char *, int);
  864.   char *get_history_word_specifier (char *spec, char *from, int *caller_index);
  865.  
  866.   /* The output string, and its length. */
  867.   int len = 0;
  868.   char *result = (char *)NULL;
  869.  
  870.   /* Used in add_string; */
  871.   char *temp, tt[2], tbl[3];
  872.  
  873.   /* Prepare the buffer for printing error messages. */
  874.   result = (char *)xmalloc (len = 255);
  875.  
  876.   result[0] = tt[1] = tbl[2] = '\0';
  877.   tbl[0] = '\\';
  878.   tbl[1] = hl_history_expansion_char;
  879.  
  880.   /* Grovel the string.  Only backslash can quote the history escape
  881.      character.  We also handle arg specifiers. */
  882.  
  883.   /* Before we grovel forever, see if the hl_history_expansion_char appears
  884.      anywhere within the text. */
  885.  
  886.   /* The quick substitution character is a history expansion all right.  That
  887.      is to say, "^this^that^" is equivalent to "!!:s^this^that^", and in fact,
  888.      that is the substitution that we do. */
  889.   if (string[0] == hl_history_subst_char)
  890.     {
  891.       char *format_string = (char *)alloca (10 + strlen (string));
  892.  
  893.       sprintf (format_string, "%c%c:s%s",
  894.            hl_history_expansion_char, hl_history_expansion_char,
  895.            string);
  896.       string = format_string;
  897.       l += 4;
  898.       goto grovel;
  899.     }
  900.  
  901.   /* If not quick substitution, still maybe have to do expansion. */
  902.  
  903.   /* `!' followed by one of the characters in hl_history_no_expand_chars
  904.      is NOT an expansion. */
  905.   for (i = 0; string[i]; i++)
  906.     if (string[i] == hl_history_expansion_char)
  907.       if (!string[i + 1] || member (string[i + 1], hl_history_no_expand_chars))
  908.     continue;
  909.       else
  910.     goto grovel;
  911.  
  912.   free (result);
  913.   *output = savestring (string);
  914.   return (0);
  915.  
  916.  grovel:
  917.  
  918.   for (i = j = 0; i < l; i++)
  919.     {
  920.       int tchar = string[i];
  921.       if (tchar == hl_history_expansion_char)
  922.     tchar = -3;
  923.  
  924.       switch (tchar)
  925.     {
  926.     case '\\':
  927.       if (string[i + 1] == hl_history_expansion_char)
  928.         {
  929.           i++;
  930.           temp = tbl;
  931.           goto do_add;
  932.         }
  933.       else
  934.         goto add_char;
  935.  
  936.       /* case hl_history_expansion_char: */
  937.     case -3:
  938.       starting_index = i + 1;
  939.       cc = string[i + 1];
  940.  
  941.       /* If the hl_history_expansion_char is followed by one of the
  942.          characters in hl_history_no_expand_chars, then it is not a
  943.          candidate for expansion of any kind. */
  944.       if (member (cc, hl_history_no_expand_chars))
  945.         goto add_char;
  946.  
  947.       /* There is something that is listed as a `word specifier' in csh
  948.          documentation which means `the expanded text to this point'.
  949.          That is not a word specifier, it is an event specifier. */
  950.  
  951.       if (cc == '#')
  952.         goto hack_pound_sign;
  953.  
  954.       /* If it is followed by something that starts a word specifier,
  955.          then !! is implied as the event specifier. */
  956.  
  957.       if (member (cc, ":$*%^"))
  958.         {
  959.           char fake_s[3];
  960.           int fake_i = 0;
  961.           i++;
  962.           fake_s[0] = fake_s[1] = hl_history_expansion_char;
  963.           fake_s[2] = '\0';
  964.           event = hl_get_history_event (fake_s, &fake_i, 0);
  965.         }
  966.       else
  967.         {
  968.           int quoted_search_delimiter = 0;
  969.  
  970.           /* If the character before this `!' is a double or single
  971.          quote, then this expansion takes place inside of the
  972.          quoted string.  If we have to search for some text ("!foo"),
  973.          allow the delimiter to end the search string. */
  974.           if (i && (string[i - 1] == '\'' || string[i - 1] == '"'))
  975.         quoted_search_delimiter = string[i - 1];
  976.  
  977.           event = hl_get_history_event (string, &i, quoted_search_delimiter);
  978.         }
  979.       
  980.       if (!event)
  981.       event_not_found:
  982.         {
  983.         int ll = 1 + (i - starting_index);
  984.  
  985.         temp = (char *)alloca (1 + ll);
  986.         strncpy (temp, string + starting_index, ll);
  987.         temp[ll - 1] = 0;
  988.         sprintf (result, "%s: %s.", temp,
  989.              word_spec_error ? "Bad word specifier" : "Event not found");
  990.         *output = result;
  991.         return (-1);
  992.       }
  993.  
  994.       /* If a word specifier is found, then do what that requires. */
  995.       starting_index = i;
  996.  
  997.       word_spec = get_history_word_specifier (string, event, &i);
  998.  
  999.       /* There is no such thing as a `malformed word specifier'.  However,
  1000.          it is possible for a specifier that has no match.  In that case,
  1001.          we complain. */
  1002.       if (word_spec == (char *)-1)
  1003.       {
  1004.         word_spec_error++;
  1005.         goto event_not_found;
  1006.       }
  1007.  
  1008.       /* If no word specifier, than the thing of interest was the event. */
  1009.       if (!word_spec)
  1010.         temp = event;
  1011.       else
  1012.         {
  1013.           temp = (char *)alloca (1 + strlen (word_spec));
  1014.           strcpy (temp, word_spec);
  1015.           free (word_spec);
  1016.         }
  1017.  
  1018.       /* Perhaps there are other modifiers involved.  Do what they say. */
  1019.  
  1020.     hack_specials:
  1021.  
  1022.       if (string[i] == ':')
  1023.         {
  1024.           char *tstr;
  1025.  
  1026.           switch (string[i + 1])
  1027.         {
  1028.           /* :p means make this the last executed line.  So we
  1029.              return an error state after adding this line to the
  1030.              history. */
  1031.         case 'p':
  1032.           only_printing++;
  1033.           goto next_special;
  1034.  
  1035.           /* :t discards all but the last part of the pathname. */
  1036.         case 't':
  1037.           tstr = strrchr (temp, '/');
  1038.           if (tstr)
  1039.             temp = ++tstr;
  1040.           goto next_special;
  1041.  
  1042.           /* :h discards the last part of a pathname. */
  1043.         case 'h':
  1044.           tstr = strrchr (temp, '/');
  1045.           if (tstr)
  1046.             *tstr = '\0';
  1047.           goto next_special;
  1048.  
  1049.           /* :r discards the suffix. */
  1050.         case 'r':
  1051.           tstr = strrchr (temp, '.');
  1052.           if (tstr)
  1053.             *tstr = '\0';
  1054.           goto next_special;
  1055.  
  1056.           /* :e discards everything but the suffix. */
  1057.         case 'e':
  1058.           tstr = strrchr (temp, '.');
  1059.           if (tstr)
  1060.             temp = tstr;
  1061.           goto next_special;
  1062.  
  1063.           /* :s/this/that substitutes `this' for `that'. */
  1064.           /* :gs/this/that substitutes `this' for `that' globally. */
  1065.         case 'g':
  1066.           if (string[i + 2] == 's')
  1067.             {
  1068.               i++;
  1069.               substitute_globally = 1;
  1070.               goto substitute;
  1071.             }
  1072.           else
  1073.             
  1074.         case 's':
  1075.           substitute:
  1076.           {
  1077.             char *this, *that, *new_event;
  1078.             int delimiter = 0;
  1079.             int si, l_this, l_that, l_temp = strlen (temp);
  1080.  
  1081.             if (i + 2 < strlen (string))
  1082.               delimiter = string[i + 2];
  1083.  
  1084.             if (!delimiter)
  1085.               break;
  1086.  
  1087.             i += 3;
  1088.  
  1089.             /* Get THIS. */
  1090.             for (si = i; string[si] && string[si] != delimiter; si++);
  1091.             l_this = (si - i);
  1092.             this = (char *)alloca (1 + l_this);
  1093.             strncpy (this, string + i, l_this);
  1094.             this[l_this] = '\0';
  1095.  
  1096.             i = si;
  1097.             if (string[si])
  1098.               i++;
  1099.  
  1100.             /* Get THAT. */
  1101.             for (si = i; string[si] && string[si] != delimiter; si++);
  1102.             l_that = (si - i);
  1103.             that = (char *)alloca (1 + l_that);
  1104.             strncpy (that, string + i, l_that);
  1105.             that[l_that] = '\0';
  1106.  
  1107.             i = si;
  1108.             if (string[si]) i++;
  1109.  
  1110.             /* Ignore impossible cases. */
  1111.             if (l_this > l_temp)
  1112.               goto cant_substitute;
  1113.  
  1114.             /* Find the first occurrence of THIS in TEMP. */
  1115.             si = 0;
  1116.             for (; (si + l_this) <= l_temp; si++)
  1117.               if (strncmp (temp + si, this, l_this) == 0)
  1118.             {
  1119.               new_event =
  1120.                 (char *)alloca (1 + (l_that - l_this) + l_temp);
  1121.               strncpy (new_event, temp, si);
  1122.               strncpy (new_event + si, that, l_that);
  1123.               strncpy (new_event + si + l_that,
  1124.                    temp + si + l_this,
  1125.                    l_temp - (si + l_this));
  1126.               new_event[(l_that - l_this) + l_temp] = '\0';
  1127.               temp = new_event;
  1128.  
  1129.               if (substitute_globally)
  1130.                 {
  1131.                   si += l_that;
  1132.                   l_temp = strlen (temp);
  1133.                   substitute_globally++;
  1134.                   continue;
  1135.                 }
  1136.  
  1137.               goto hack_specials;
  1138.             }
  1139.  
  1140.           cant_substitute:
  1141.  
  1142.             if (substitute_globally > 1)
  1143.               {
  1144.             substitute_globally = 0;
  1145.             goto hack_specials;
  1146.               }
  1147.  
  1148.             goto event_not_found;
  1149.           }
  1150.  
  1151.           /* :# is the line so far.  Note that we have to
  1152.              alloca () it since RESULT could be realloc ()'ed
  1153.              below in add_string. */
  1154.         case '#':
  1155.         hack_pound_sign:
  1156.           if (result)
  1157.             {
  1158.               temp = (char *)alloca (1 + strlen (result));
  1159.               strcpy (temp, result);
  1160.             }
  1161.           else
  1162.             temp = "";
  1163.  
  1164.         next_special:
  1165.           i += 2;
  1166.           goto hack_specials;
  1167.         }
  1168.  
  1169.         }
  1170.       /* Believe it or not, we have to back the pointer up by one. */
  1171.       --i;
  1172.       goto add_string;
  1173.  
  1174.       /* A regular character.  Just add it to the output string. */
  1175.     default:
  1176.     add_char:
  1177.       tt[0] = string[i];
  1178.       temp = tt;
  1179.       goto do_add;
  1180.  
  1181.     add_string:
  1182.       modified++;
  1183.  
  1184.     do_add:
  1185.       j += strlen (temp);
  1186.       while (j > len)
  1187.         result = (char *)xrealloc (result, (len += 255));
  1188.  
  1189.       strcpy (result + (j - strlen (temp)), temp);
  1190.     }
  1191.     }
  1192.  
  1193.   *output = result;
  1194.  
  1195.   if (only_printing)
  1196.     {
  1197.       hl_add_history (result);
  1198.       return (-1);
  1199.     }
  1200.  
  1201.   return (modified != 0);
  1202. }
  1203.  
  1204. /* Return a consed string which is the word specified in SPEC, and found
  1205.    in FROM.  NULL is returned if there is no spec.  -1 is returned if
  1206.    the word specified cannot be found.  CALLER_INDEX is the offset in
  1207.    SPEC to start looking; it is updated to point to just after the last
  1208.    character parsed. */
  1209. static char *
  1210. get_history_word_specifier (char *spec, char *from, int *caller_index)
  1211. {
  1212.   register int i = *caller_index;
  1213.   int first, last;
  1214.   int expecting_word_spec = 0;
  1215.   char *hl_history_arg_extract (int first, int last, char *string);
  1216.  
  1217.   /* The range of words to return doesn't exist yet. */
  1218.   first = last = 0;
  1219.  
  1220.   /* If we found a colon, then this *must* be a word specification.  If
  1221.      it isn't, then it is an error. */
  1222.   if (spec[i] == ':')
  1223.     i++, expecting_word_spec++;
  1224.  
  1225.   /* Handle special cases first. */
  1226.  
  1227.   /* `%' is the word last searched for. */
  1228.   if (spec[i] == '%')
  1229.     {
  1230.       *caller_index = i + 1;
  1231.       if (search_string)
  1232.     return (savestring (search_string));
  1233.       else
  1234.     return (savestring (""));
  1235.     }
  1236.  
  1237.   /* `*' matches all of the arguments, but not the command. */
  1238.   if (spec[i] == '*')
  1239.     {
  1240.       char *star_result;
  1241.  
  1242.       *caller_index = i + 1;
  1243.       star_result = hl_history_arg_extract (1, '$', from);
  1244.  
  1245.       if (!star_result)
  1246.     star_result = savestring ("");
  1247.  
  1248.       return (star_result);
  1249.     }
  1250.  
  1251.   /* `$' is last arg. */
  1252.   if (spec[i] == '$')
  1253.     {
  1254.       *caller_index = i + 1;
  1255.       return (hl_history_arg_extract ('$', '$', from));
  1256.     }
  1257.  
  1258.   /* Try to get FIRST and LAST figured out. */
  1259.   if (spec[i] == '-' || spec[i] == '^')
  1260.     {
  1261.       first = 1;
  1262.       goto get_last;
  1263.     }
  1264.  
  1265.   if (digit (spec[i]) && expecting_word_spec)
  1266.     {
  1267.       sscanf (spec + i, "%d", &first);
  1268.       for (; digit (spec[i]); i++);
  1269.     }
  1270.   else
  1271.     return ((char *)NULL);
  1272.  
  1273.  get_last:
  1274.   if (spec[i] == '^')
  1275.     {
  1276.       i++;
  1277.       last = 1;
  1278.       goto get_args;
  1279.     }
  1280.  
  1281.   if (spec[i] != '-')
  1282.     {
  1283.       last = first;
  1284.       goto get_args;
  1285.     }
  1286.  
  1287.   i++;
  1288.  
  1289.   if (digit (spec[i]))
  1290.     {
  1291.       sscanf (spec + i, "%d", &last);
  1292.       for (; digit (spec[i]); i++);
  1293.     }
  1294.   else
  1295.     if (spec[i] == '$')
  1296.       {
  1297.     i++;
  1298.     last = '$';
  1299.       }
  1300.  
  1301.  get_args:
  1302.   {
  1303.     char *result = (char *)NULL;
  1304.  
  1305.     *caller_index = i;
  1306.  
  1307.     if (last >= first)
  1308.       result = hl_history_arg_extract (first, last, from);
  1309.  
  1310.     if (result)
  1311.       return (result);
  1312.     else
  1313.       return ((char *)-1);
  1314.   }
  1315. }
  1316.  
  1317. /* Extract the args specified, starting at FIRST, and ending at LAST.
  1318.    The args are taken from STRING.  If either FIRST or LAST is < 0,
  1319.    then make that arg count from the right (subtract from the number of
  1320.    tokens, so that FIRST = -1 means the next to last token on the line). */
  1321. char *
  1322. hl_history_arg_extract (int first, int last, char *string)
  1323. {
  1324.   register int i, len;
  1325.   char *result = (char *)NULL;
  1326.   int size = 0, offset = 0;
  1327.  
  1328.   char **history_tokenize (char *), **list;
  1329.  
  1330.   if (!(list = history_tokenize (string)))
  1331.     return ((char *)NULL);
  1332.  
  1333.   for (len = 0; list[len]; len++);
  1334.  
  1335.   if (last < 0)
  1336.     last = len + last - 1;
  1337.  
  1338.   if (first < 0)
  1339.     first = len + first - 1;
  1340.  
  1341.   if (last == '$')
  1342.     last = len - 1;
  1343.  
  1344.   if (first == '$')
  1345.     first = len - 1;
  1346.  
  1347.   last++;
  1348.  
  1349.   if (first > len || last > len || first < 0 || last < 0)
  1350.     result = ((char *)NULL);
  1351.   else
  1352.     {
  1353.       for (i = first; i < last; i++)
  1354.     {
  1355.       int l = strlen (list[i]);
  1356.  
  1357.       if (!result)
  1358.         result = (char *)xmalloc ((size = (2 + l)));
  1359.       else
  1360.         result = (char *)xrealloc (result, (size += (2 + l)));
  1361.       strcpy (result + offset, list[i]);
  1362.       offset += l;
  1363.       if (i + 1 < last)
  1364.         {
  1365.           strcpy (result + offset, " ");
  1366.           offset++;
  1367.         }
  1368.     }
  1369.     }
  1370.  
  1371.   for (i = 0; i < len; i++)
  1372.     free (list[i]);
  1373.  
  1374.   free (list);
  1375.  
  1376.   return (result);
  1377. }
  1378.  
  1379. #define slashify_in_quotes "\\`\"$"
  1380.  
  1381. /* Return an array of tokens, much as the shell might.  The tokens are
  1382.    parsed out of STRING. */
  1383. static char **
  1384. history_tokenize (char *string)
  1385. {
  1386.   char **result = (char **)NULL;
  1387.   register int i, start, result_index, size;
  1388.   int len;
  1389.  
  1390.   i = result_index = size = 0;
  1391.  
  1392.   /* Get a token, and stuff it into RESULT.  The tokens are split
  1393.      exactly where the shell would split them. */
  1394.  get_token:
  1395.  
  1396.   /* Skip leading whitespace. */
  1397.   for (; string[i] && whitespace(string[i]); i++);
  1398.  
  1399.   start = i;
  1400.  
  1401.   if (!string[i] || string[i] == hl_history_comment_char)
  1402.     return (result);
  1403.  
  1404.   if (member (string[i], "()\n")) {
  1405.     i++;
  1406.     goto got_token;
  1407.   }
  1408.  
  1409.   if (member (string[i], "<>;&|")) {
  1410.     int peek = string[i + 1];
  1411.  
  1412.     if (peek == string[i]) {
  1413.       if (peek ==  '<') {
  1414.     if (string[1 + 2] == '-')
  1415.       i++;
  1416.     i += 2;
  1417.     goto got_token;
  1418.       }
  1419.  
  1420.       if (member (peek, ">:&|")) {
  1421.     i += 2;
  1422.     goto got_token;
  1423.       }
  1424.     } else {
  1425.       if ((peek == '&' &&
  1426.       (string[i] == '>' || string[i] == '<')) ||
  1427.       ((peek == '>') &&
  1428.       (string[i] == '&'))) {
  1429.     i += 2;
  1430.     goto got_token;
  1431.       }
  1432.     }
  1433.     i++;
  1434.     goto got_token;
  1435.   }
  1436.  
  1437.   /* Get word from string + i; */
  1438.   {
  1439.     int delimiter = 0;
  1440.  
  1441.     if (member (string[i], "\"'`"))
  1442.       delimiter = string[i++];
  1443.  
  1444.     for (;string[i]; i++) {
  1445.  
  1446.       if (string[i] == '\\') {
  1447.  
  1448.     if (string[i + 1] == '\n') {
  1449.       i++;
  1450.       continue;
  1451.     } else {
  1452.       if (delimiter != '\'')
  1453.         if ((delimiter != '"') ||
  1454.         (member (string[i], slashify_in_quotes))) {
  1455.           i++;
  1456.           continue;
  1457.         }
  1458.     }
  1459.       }
  1460.  
  1461.       if (delimiter && string[i] == delimiter) {
  1462.     delimiter = 0;
  1463.     continue;
  1464.       }
  1465.  
  1466.       if (!delimiter && (member (string[i], " \t\n;&()|<>")))
  1467.     goto got_token;
  1468.  
  1469.       if (!delimiter && member (string[i], "\"'`")) {
  1470.     delimiter = string[i];
  1471.     continue;
  1472.       }
  1473.     }
  1474.     got_token:
  1475.  
  1476.     len = i - start;
  1477.     if (result_index + 2 >= size) {
  1478.       if (!size)
  1479.     result = (char **)xmalloc ((size = 10) * (sizeof (char *)));
  1480.       else
  1481.     result =
  1482.       (char **)xrealloc (result, ((size += 10) * (sizeof (char *))));
  1483.     }
  1484.     result[result_index] = (char *)xmalloc (1 + len);
  1485.     strncpy (result[result_index], string + start, len);
  1486.     result[result_index][len] = '\0';
  1487.     result_index++;
  1488.     result[result_index] = (char *)NULL;
  1489.   }
  1490.   if (string[i])
  1491.     goto get_token;
  1492.  
  1493.   return (result);
  1494. }
  1495.  
  1496. #if defined (STATIC_MALLOC)
  1497.  
  1498. /* **************************************************************** */
  1499. /*                                    */
  1500. /*            xmalloc and xrealloc ()                     */
  1501. /*                                    */
  1502. /* **************************************************************** */
  1503.  
  1504. static void memory_error_and_abort (void);
  1505.  
  1506. static char *
  1507. xmalloc (int bytes)
  1508. {
  1509.   char *temp = (char *)malloc (bytes);
  1510.  
  1511.   if (!temp)
  1512.     memory_error_and_abort ();
  1513.   return (temp);
  1514. }
  1515.  
  1516. static char *
  1517. xrealloc (void *pointer, int bytes)
  1518. {
  1519.   char *temp;
  1520.  
  1521.   if (!pointer)
  1522.     temp = (char *)xmalloc (bytes);
  1523.   else
  1524.     temp = (char *)realloc (pointer, bytes);
  1525.  
  1526.   if (!temp)
  1527.     memory_error_and_abort ();
  1528.  
  1529.   return (temp);
  1530. }
  1531.  
  1532. static void
  1533. memory_error_and_abort (void)
  1534. {
  1535.   fprintf (stderr, "history: Out of virtual memory!\n");
  1536.   abort ();
  1537. }
  1538. #endif /* STATIC_MALLOC */
  1539.  
  1540.